<?php
require('config.php');
// utility functions
function directoryToArray($directory, $recursive) {
	$array_items = array();
	if ($handle = opendir($directory)) {
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != "..") {
				if (is_dir($directory. "/" . $file)) {
					if($recursive) {
						$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
					}
					$file = $directory . "/" . $file;
					$array_items[] = preg_replace("/\/\//si", "/", $file);
				} else {
					$file = $directory . "/" . $file;
					$array_items[] = preg_replace("/\/\//si", "/", $file);
				}
			}
		}
		closedir($handle);
	}
	return $array_items;
}

function show_all_tags($item_id,$dbfilename) {
// list all tags 	
	$db=new SQLite3($dbfilename);
	$query="SELECT t.* FROM tag_t t, map_t m WHERE (m.item_id='$item_id' AND t.tag_id=m.tag_id) GROUP BY t.tag_id;";
	$results=$db->query($query);

	while($res = $results->fetchArray(SQLITE3_ASSOC)){
		$tag = $res['value'];
		$tag_id = $res['tag_id'];
		show_tag($item_id,$tag_id,$tag); 
	}
	
	
	$db->close();
	
}

function display_tag($item_id,$tag_id,$tag) {
	$tag_parts=explode('::',$tag);
	$tag_type=$tag_parts[0];
	$tag_value=$tag_parts[1];
	$str="<span class=\"$tag_type-tag\" id=\"tag_id$tag_id\">" .
			"<a href=\"list_items_by_tag.php?q=$tag_id\">" .$tag_value . "</a>";
	$str.="<a href=\"delete_tag.php?item_id=$item_id&tag_id=$tag_id\" class=\"remove_tag\" title=\"remove tag\">x</a>";
	
	$str.="</span>";
	echo $str;
	
}

function show_item($item_id,$dbfilename,$full)
{
	// get all tags
	$db=new SQLite3($dbfilename);
	$query="SELECT t.* FROM tag_t t, map_t m WHERE (m.item_id='$item_id' AND t.tag_id=m.tag_id) GROUP BY t.tag_id;";
	$results=$db->query($query);
	
	$keywords=0;
	while($res = $results->fetchArray(SQLITE3_ASSOC)){
		$tag_id=$res['tag_id'];
		$tag_value=$res['value'];
		$tags[$tag_id]=$tag_value;
		
		$tag_parts=explode('::',$tag_value);
		// type::value
		switch ($tag_parts[0]) {
			case 'author':
				$authors[] = $tag_id;
				break;
			case 'title':
				$titles[] = $tag_id;
				break;
			case 'publisher':
				$publishers[] = $tag_id;
				break;
			case 'year':
				$years[] = $tag_id;
				break;		
			default: /* free/unknown keyword*/
				$keywords[] = $tag_id;
				break;								
		}					
	}
	// get physical filename
	$query="SELECT pfn FROM item_t WHERE (item_id='$item_id');";
	$results=$db->query($query);
	while($res = $results->fetchArray(SQLITE3_ASSOC)){
		$pfn=$res['pfn'];
	}
	echo "<div class=\"item\">";
	echo "<span class=\"tag-author\">"; 
	if (!empty($authors)) {
		foreach ($authors as $idx) {
		display_tag($item_id,$idx,$tags[$idx]);
		}
	} else {
		echo "display_missing('author')";
	}
	echo ": </span><br>";
	/*
	echo "<span class=\"tag-title\"> <a href=\"$pfn\">";
	if (isset($tags['title'])) {
		 echo $tags['title'];
	} else {
		echo basename($pfn); 
	}
	echo "</a></span><br>";
	
	echo "<span class=\"tag-publisher\">";
	if (isset($tags['publisher'])) {
		echo $tags['publisher'];
	}
	echo "</span>";
	
	echo "<span class=\"tag-year\">";
	if (isset($tags['year'])) {
		echo " (" . $tags['year'] . ")";
	}
	echo "</span>";
*/
	foreach ($free_keywords as $keyword) {
		echo $keyword . "&nbsp;";
	}
	echo "<br>";
	
	echo "</div><br>";
	
	//show_all_tags($item_id,$dbfilename);
	echo "<br>";
}


